package com.relax.mybatis.plugin.sensitive.strategy.impl;

import org.apache.commons.lang3.StringUtils;

import com.relax.mybatis.plugin.sensitive.strategy.SensitiveStrategy;

/**
 * @author relax
 * @date 2022/07/05
 * @desc 默认脱敏策略
 */
public class DefaultSensitiveStrategy implements SensitiveStrategy {

    /**
     * <p>默认脱敏：只显示首尾</p>
     *
     * <pre>
     * DefaultSensitiveStrategy.handle("AB")        = "AB"
     * DefaultSensitiveStrategy.handle("123456789") = "1*******9"
     * </pre>
     *
     * @param data
     * @param symbol
     * @return
     */
    @Override
    public String handle(String data, String symbol) {
        int length = StringUtils.length(data);
        if (length <= 2) {
            return data;
        }
        return StringUtils.rightPad(StringUtils.left(data, 1), length - 1, symbol)
                .concat(StringUtils.right(data, 1));
    }

}
